package domain.model.project.support.projectscm;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import domain.model.project.ProjectSCM;
import domain.model.project.support.RWProjectSCM;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author Gregory Boissinot
 */
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ProjectSCMImpl implements RWProjectSCM {

    @JsonProperty(required = true, value = "url")
    private String scmConnection;
    @JsonProperty(required = false)
    private String userName;
    @JsonProperty(required = false)
    private String password;

    @Override
    public String getScmConnection() {
        return scmConnection;
    }

    @Override
    public void setScmConnection(String scmConnection) {
        this.scmConnection = scmConnection;
    }

    @Override
    public String getUserName() {
        return userName;
    }

    @Override
    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean sameValueAs(ProjectSCM objectValue) {
        return equals(objectValue);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ProjectSCMImpl that = (ProjectSCMImpl) o;

        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (scmConnection != null ? !scmConnection.equals(that.scmConnection) : that.scmConnection != null)
            return false;
        if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = scmConnection != null ? scmConnection.hashCode() : 0;
        result = 31 * result + (userName != null ? userName.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(this,
                ToStringStyle.MULTI_LINE_STYLE);
        return reflectionToStringBuilder.toString();
    }
}
